import { getCartInfo, del } from "../assets/request.js"
//用户信息相关的vuex的js
export default {
  namespaced: true,
  state: {
     //存储所有商品信息
        goods: [],
  },
  mutations: {
     //发送请求获取购物车信息
     getShop(state,value){
         state.goods = value;       
     },     
  },
  actions: {
     //获取购物车信息
     async getShops({commit},value){
        let cartInfomation = await getCartInfo({
            project_id: 260,
          })
          commit("getShop",cartInfomation.data.result);                   
      },


    // 删除该项商品
    // async deleteOne(id) {
    //     //console.log(id);
    //     let delRes = await del(id);
  
    //     if (confirm("确定删除该商品吗,亲亲?")) {
    //       //更新本地页面
    //       let cartInfomation = await getCartInfo({
    //         project_id: 260,
    //       });
    //       //console.log(cartInfomation);
    //       this.goods = cartInfomation.data.result;
    //     }
  
    //   },

  },
  getters: {

      //总数量
      totalSum(state) {
        return state.goods.filter((item) => item.select)
          .reduce((prev, item) => prev += item.num
            , 0)
      },
      //总价
      totalMoney(state) {
        return state.goods.filter((item) => item.select)
          .reduce((prev, good) => prev += good.s_good.price * good.num
            , 0)
  
  
      },
  
  },
};
